//
//  job_control_block.h
//  Job_Scheduling
//
//  Created by zhangyukun on 2021/4/21.
//

#ifndef job_control_block_h
#define job_control_block_h

#include "list_head.h"
#include <stdlib.h>
#include <string.h>
/*
This head file is to create JCB , initialize it and provide certain function to operate on JCB .
**/

//struct JobControlBlock is to store some information of one job .
struct JobControlBlock{
    int number;                     //job's id .
    int reach_time;                 //job's reach time .
    int need_time;                  //job's need time .
    int privilege;                  //job's privilege .
    double response_ratio;          //job's response ratio .
    int start_time;                 //job's start time .
    int wait_time;                  //job's total wait time .
    int run_time;                   //job's run time .
    int turnaround_time;            //job's turnaround time .
    double weighted_turnaround_time;//job's weighted turnaround time .
    int visited;                    //to test whether this job is visted .
    bool isreached;                 //to test whether this job is reached .
    //struct list_head is to constitute one double-linked circular list .
    struct list_head list;
};
//to count the number of JCBs .
int length = 0;

//read data from file to struct JobControlBlock .
void read_data(struct list_head *head){
    struct JobControlBlock JCB;
    FILE *fp;
//    fp = fopen("/Applications/LANGUAGE/C/Job_Scheduling/Job_Scheduling/test_robin.txt", "r");
//    fp = fopen("/Applications/LANGUAGE/C/Job_Scheduling/Job_Scheduling/test.txt", "r");
    fp = fopen("test_robin.txt", "r");
    //test whether the file is open .
    if(fp == NULL){
        printf("ERROR\nCan not open the file .\n");
        exit(1);
    }
    //read data and constitute the waiting list .
    while(fscanf(fp, "%d %d %d %d",&JCB.number,&JCB.reach_time,&JCB.need_time,&JCB.privilege) != EOF){
        struct JobControlBlock *ptr = malloc(sizeof(struct JobControlBlock));
        ptr->number = JCB.number;
        ptr->reach_time = JCB.reach_time;
        ptr->need_time = JCB.need_time;
        ptr->privilege = JCB.privilege;
        //initialize the struct list_head and add to waiting list .
        INIT_LIST_HEAD(&(ptr->list));
        add(head->prev, &ptr->list);
        length++;
    }
    //close the file .
    fclose(fp);
}
//initialize the struct JobControlBlock in waiting list .
void init_JCB(struct list_head *head){
    struct list_head *curr;
    struct JobControlBlock *ptr;
    //tranverse the wait list .
    list_for_each(curr, head){
        //find the start address of struct JobControlBlock .
        ptr = list_entry(curr, struct JobControlBlock,list);
        //initialize the JCB .
        ptr->response_ratio = 0;
        ptr->start_time = ptr->reach_time;
        ptr->wait_time = 0;
        ptr->run_time = 0;
        ptr->turnaround_time = 0;
        ptr->weighted_turnaround_time = 0;
        ptr->visited = 0;
        ptr->isreached = false;
    }
}
//print certain information of JCB .
void Print_JCB(struct list_head *head){
    struct list_head *curr;
    struct JobControlBlock *ptr = NULL;
    printf("进程控制块信息：\n");
    list_for_each(curr, head){
        ptr = list_entry(curr, struct JobControlBlock,list);
        printf("%1d\t%3d\t%2d\t%1d\n",ptr->number,ptr->reach_time,ptr->need_time,ptr->privilege);
    }
}

#endif /* job_control_block_h */
